package pl.mchaniewski.ryneczek.web.user;

import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;

import pl.mchaniewski.ryneczek.pojo.Order;
import pl.mchaniewski.ryneczek.services.OrderService;
import pl.mchaniewski.ryneczek.utilities.NavigationUtil;

@Component
@Scope("request")
@PreAuthorize("isAuthenticated()")
public class ArchiveOrdersListBean {
    private SelectedPortfolioBean selectedPortfolioBean;
    private OrderService orderService;

    /* PostConstruct parameters */
    private List<Order> orders;

    @Autowired
    public void setSelectedPortfolioBean(
            SelectedPortfolioBean selectedPortfolioBean) {
        this.selectedPortfolioBean = selectedPortfolioBean;
    }

    @Autowired
    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }

    public List<Order> getOrders() {
        return orders;
    }

    @PostConstruct
    public void init() {
        if (selectedPortfolioBean.getSelectedPortfolioId() == null) {
            NavigationUtil.directRedirect(NavigationUtil.DIRECT_INDEX);
        }

        updateOrderList();
    }

    private void updateOrderList() {
        orders = orderService
                .findAllButAcceptedByPortfolioId(selectedPortfolioBean
                        .getSelectedPortfolioId());
    }

}
